package pl.edu.agh.student.model;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.hibernate.validator.Length;
import org.jboss.seam.annotations.Name;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

@Entity
@Name("applicationx")
@Table(name = "applications")
public class Application extends AbstractEntity {

	private String name;
	private BigDecimal amount;
	private Customer person;
	private Applicant applicant;
	private State state = State.NEW;
	private Set<Attachment> attachments = new HashSet<Attachment>();
	private List<BikObligation> bikObligations = new ArrayList<BikObligation>();

	public Application() {
		super();
	}

	@Length(max = 40)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne
	@JoinColumn(name = "personId")
	public Customer getPerson() {
		return person;
	}

	public void setPerson(Customer person) {
		this.person = person;
	}

	@Embedded
	public Applicant getApplicant() {
		if (applicant == null)
			applicant = new Applicant();
		return applicant;
	}

	public void setApplicant(Applicant applicant) {
		this.applicant = applicant;
	}

	// @NotNull
	// @Pattern(regex="\\d+")
	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "process_state", nullable = true)
	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	@OneToMany(mappedBy = "application", targetEntity = Attachment.class)
	public Set<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(Set<Attachment> attachments) {
		this.attachments = attachments;
	}
	
	@OneToMany(mappedBy = "application", targetEntity = BikObligation.class)
	@OrderBy("month DESC")
	public List<BikObligation> getBikObligations() {
		return bikObligations;
	}

	public void setBikObligations(List<BikObligation> bikObligations) {
		this.bikObligations = bikObligations;
	}
}
